package web.sso.model.http;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import web.sso.model.AbstractModel;

/**
 * DATE: 2020/1/28
 * TIME: 3:40 下午
 * AUTHOR: hlm
 */
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class Response<T> extends AbstractModel<Response<T>> {

    private int code;
    private String msg;
    private long ts;
    private T data;

    /**
     * 各种响应状态的结构构造
     * 服务状态码参考HTTP状态码实现
     */
    public static <T> Response<T> ok(T data, String msg) {
        return buildResponse(200, msg, data);
    }

    public static <T> Response<T> ok(T data) {
        return ok(data, "ok");
    }

    public static <T> Response<T> ok(String msg) {
        return ok(null, msg);
    }

    public static <T> Response<T> accepted(T data, String msg) {
        return buildResponse(201, msg, data);
    }

    public static <T> Response<T> accepted(T data) {
        return accepted(data, "accepted");
    }

    public static <T> Response<T> accepted(String msg) {
        return accepted(null, msg);
    }

    public static <T> Response<T> badRequest(T data, String msg) {
        return buildResponse(400, msg, data);
    }

    public static <T> Response<T> badRequest(T data) {
        return badRequest(data, "bad request");
    }

    public static <T> Response<T> badRequest(String msg) {
        return badRequest(null, msg);
    }

    public static <T> Response<T> unauthorized(T data, String msg) {
        return buildResponse(401, msg, data);
    }

    public static <T> Response<T> unauthorized(T data) {
        return unauthorized(data, "unauthorized");
    }

    public static <T> Response<T> unauthorized(String msg) {
        return unauthorized(null, msg);
    }

    public static <T> Response<T> forbidden(T data, String msg) {
        return buildResponse(403, msg, data);
    }

    public static <T> Response<T> forbidden(T data) {
        return forbidden(data, "forbidden");
    }

    public static <T> Response<T> forbidden(String msg) {
        return forbidden(null, msg);
    }

    public static <T> Response<T> notFound(T data, String msg) {
        return buildResponse(404, msg, data);
    }

    public static <T> Response<T> notFound(T data) {
        return notFound(data, "not found");
    }

    public static <T> Response<T> notFound(String msg) {
        return notFound(null, msg);
    }

    public static <T> Response<T> serverError(T data, String msg) {
        return buildResponse(500, msg, data);
    }

    public static <T> Response<T> serverError(T data) {
        return serverError(data, "server error");
    }

    public static <T> Response<T> serverError(String msg) {
        return serverError(null, msg);
    }

    public static <T> Response<T> serviceUnavailable(T data, String msg) {
        return buildResponse(503, msg, data);
    }

    public static <T> Response<T> serviceUnavailable(T data) {
        return serviceUnavailable(data, "service unavailable");
    }

    public static <T> Response<T> serviceUnavailable(String msg) {
        return serviceUnavailable(null, msg);
    }
    /**
     * 构造结束
     */

    /**
     * 构建Response对象
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    private static <T> Response<T> buildResponse(int code, String msg, T data) {
        Response<T> response = new Response<>();
        response.code = code;
        response.data = data;
        response.msg = msg;
        response.ts = System.currentTimeMillis();
        return response;
    }

    /**
     * 状态判断接口
     */
    @JsonIgnore
    public boolean isOk() {
        return code == 200;
    }

    @JsonIgnore
    public boolean isAccepted() {
        return code == 201;
    }

    @JsonIgnore
    public boolean isBadRequest() {
        return code == 400;
    }

    @JsonIgnore
    public boolean isUnauthorized() {
        return code == 401;
    }

    @JsonIgnore
    public boolean isForbidden() {
        return code == 403;
    }

    @JsonIgnore
    public boolean isNotFound() {
        return code == 404;
    }

    @JsonIgnore
    public boolean isServerError() {
        return code == 500;
    }

    @JsonIgnore
    public boolean isServiceUnavailable() {
        return code == 503;
    }
    /***************/
}
